package greedy.区间选择;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class leetcode56合并区间 {
    public static int[][] merge(int[][] intervals) {
        if (intervals == null || intervals.length == 0)
            return new int[][]{};
        Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
        ArrayList<int[]>list = new ArrayList<>();
        list.add(intervals[0]);
        for (int i = 1; i < intervals.length; i++) {
            int size = list.size();
            if (intervals[i][0] > list.get(size - 1)[1]){
                list.add(intervals[i]);
            }else {
                if (intervals[i][1]>list.get(size - 1)[1]){
                    list.get(size - 1)[1] = intervals[i][1];
                }
            }
        }
        int size = list.size();
        int[][] result = new int[size][2];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
}
